/************************************************************************/
/* Mega16 EEPROM  writing and reading. 8 and 16 bit.                    */
/************************************************************************/ 
#include <avr/io.h>
#include <avr/eeprom.h>
#include <avr/interrupt.h>

void EEPROMWriteByte(uint8_t address, uint8_t data)
{
	// Disable interrupts before writing on the EEPROM.
	cli();
	// Wait for the EEPROM to be ready before writing
	eeprom_busy_wait();
	eeprom_write_byte((uint8_t*)address, data);
	// Enable interrupts
	sei();
}

uint8_t EEPROMReadByte(uint8_t address)
{
	// Wait for the EEPROM to be ready
	eeprom_busy_wait();
	return eeprom_read_byte((uint8_t*)address);
}

void EEPROMWriteWord(uint16_t address, uint16_t data)
{
	// Disable interrupts before writing on the EEPROM.
	cli();
	// Wait for the EEPROM to be ready before writing
	eeprom_busy_wait();
	eeprom_write_word((uint16_t*)address, data);
	// Enable interrupts
	sei();
}

uint16_t EEPROMReadWord(uint16_t address)
{
	// Wait for the EEPROM to be ready
	eeprom_busy_wait();
	return eeprom_read_word((uint16_t*)address);
}